job "echo" {
  datacenters = ["dc1"]
  type        = "service"

  group "echo" {
    count = 1

    scaling {
      min = 1
      max = 5

      policy {
        cooldown  = "5m"
        evaluation_interval = "30s"

        check "cpu" {
          source = "prometheus"
          query  = <<EOT
          avg(rate(container_cpu_usage_seconds_total{job="echo"}[5m]))
          /
          count(container_cpu_usage_seconds_total{job="echo"}) * 100
          EOT

          upper_threshold {
            value     = 70  # CPU使用率超过70%时扩容
            increment = 1   # 每次增加1个实例
          }

          lower_threshold {
            value     = 30  # CPU使用率低于30%时缩容
            decrement = 1   # 每次减少1个实例
          }
        }
      }
    }

    network {
      port "http" {
        to = 9999
      }
    }

    service {
      name = "echo"
      tags = ["http", "api"]
      port = "http"

      check {
        type     = "http"
        path     = "/health"
        interval = "10s"
        timeout  = "2s"
      }

      # 为Prometheus添加元数据标签
      meta {
        metrics_path = "/metrics"
        scrape       = "true"
      }
    }

    task "echo" {
      driver = "docker"

      config {
        image = "registry.cn-hangzhou.aliyuncs.com/my_public_images/echo:latest"  # 替换为你的镜像
        ports = ["http"]
      }

      resources {
        cpu    = 100  # MHz
        memory = 64  # MB
      }
    }
  }
}